package websimulator;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import buildings.House;

import readwrite.Connector;

/**
 * Servlet implementation class ModulHouseList
 */
@WebServlet("/ModulHouseList")
public class ModulHouseList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Connector con;    
	private ArrayList<House> house = new ArrayList<House>();
    /**
     * @throws Exception 
     * @see HttpServlet#HttpServlet()
     */
    public ModulHouseList() throws Exception {
        super();
        con = new Connector();
        
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int type = Integer.parseInt(request.getParameter("type"));
		String directive = "";
		try {
			house = con.GetHouseByType(type);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String answer="";
		String id="";
		 answer += "<div id=\"house\">";
		answer +=		"<table> <tr> <td><h4>People/Employees:</h4> </td> <td><h4>Area: </h4></td> <td><h4>Directiv: </h4></td> <td><h4>WallHeight: </h4></td><td><h4>Add:</h4></td> </tr>";
		for(int i = 0; i < house.size(); i++){
			try {
				directive = con.GetDirectivName(house.get(i).getDirective());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			id="houseId"+i;
		answer +=	"<tr> <td> "+house.get(i).getPeople()+" </td>  <td> "+house.get(i).getArea()+" m^2"+" </td> " +
				" <td> "+directive+" </td><td> "+house.get(i).getWallHeight()+" m"+" </td>  <td><input type=\"button\" name=\"add\" id=\"add\" value=\"Add\" onclick=\"add('"+i+"');updateList()\"></td></tr>";
		answer += "<input type=\"hidden\" name=\"id\" id=\""+id+"\" value=\""+house.get(i).getId()+"\">";
		}
		answer += "</table></div>";
		
		response.setContentType("text/plain");
		response.getWriter().write(answer);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int houseId = Integer.parseInt(request.getParameter("houseId"));
		int modulId = Integer.parseInt(request.getParameter("modulId"));
		String typeText = "";
		String directive = "";
		try {
			//int modulId = con.GetLastModulID();
			if(modulId != 0)
				
			con.AddModulHouse(houseId, modulId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		String answer="";
		String id="";
		 answer += "<div id=\"house\">";
		 answer += "<h3>You added a house!</h3>";
		 answer +=		"<table> <tr> <td><h4>People/Employees:</h4> </td> <td><h4>Area: </h4></td> <td><h4>Directiv: </h4></td> <td><h4>WallHeight: </h4></td><td><h4>Add:</h4></td> </tr>";
		for(int i = 0; i < house.size(); i++){
			id="houseId"+i;
			try {
				directive = con.GetDirectivName(house.get(i).getDirective());
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
		answer +=	"<tr> <td> "+house.get(i).getPeople()+" </td>  <td> "+house.get(i).getArea()+" m^2"+" </td> <td> "+directive+" </td> " +
				" <td> "+house.get(i).getWallHeight()+" m"+" </td>  <td><input type=\"button\" name=\"add\" id=\"add\" value=\"Add\" onclick=\"add('"+i+"')\"></td></tr>";
		answer += "<input type=\"hidden\" name=\"id\" id=\""+id+"\" value=\""+house.get(i).getId()+"\">";
		}
		answer += "</table></div>";
		
		response.setContentType("text/plain");
		response.getWriter().write(answer);
		
	}

}
